package org.sojex.photo;

import org.sojex.R;
import org.sojex.photo.bean.CookBean;
import org.sojex.photo.util.GAdapter;
import org.sojex.photo.util.GlobalData;
import org.sojex.photo.util.Util;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

public class GalleryActivity extends Activity {
	private ImageSwitcher is;
	private Gallery gallery;
	private int downX, upX;
	GAdapter adapter;
	int pos;
	Button zoomBtn,dishBtn,myOrderBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photo_gallery);
		Bundle b = getIntent().getExtras();
		pos = b.getInt("position");
		gallery = (Gallery) findViewById(R.id.gallery);
		adapter = new GAdapter(getApplicationContext(), pos);
		gallery.setAdapter(adapter);
		zoomBtn = (Button) findViewById(R.id.zoomBtn);
		dishBtn = (Button) findViewById(R.id.dishCookBtn);
		myOrderBtn = (Button) findViewById(R.id.myOrderBtn);
		is = (ImageSwitcher) findViewById(R.id.switcher1);
		new LoadGalleryTask(this, adapter).execute(pos);
		addListener();
		// // 设置ImageSwitcher控件
		is.setFactory(new ViewFactory() {

			@Override
			public View makeView() {
				// TODO Auto-generated method stub
				ImageView i = new ImageView(GalleryActivity.this);
				i.setScaleType(ImageView.ScaleType.FIT_CENTER);// 居中
				i.setLayoutParams(new ImageSwitcher.LayoutParams(// 自适应图片大小
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				return i;
			}
		});
		CookBean bean = GlobalData.cookBook.get(pos).cooks.get(0);
		if (bean.bitmap != null)
			is.setImageDrawable(new BitmapDrawable(bean.bitmap));
		else {
			try {
				if (Util.isExist(bean.uri))
					is.setImageDrawable(new BitmapDrawable(bean.uri));
				else
					is.setImageResource(R.drawable.pic);
			} catch (Exception e) {
				// TODO: handle exception
				is.setImageResource(R.drawable.pic);
			}
		}
		is.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		is.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));
	}
	public void addListener(){
		zoomBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(GalleryActivity.this,
						TouchActivity.class);
				Bundle b = new Bundle();
				b.putInt("childId", gallery.getSelectedItemPosition());
				b.putInt("groupId", pos);
				i.putExtras(b);
				startActivity(i);
			}
		});
		dishBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CookBean bean  = GlobalData.cookBook.get(pos).cooks.get(gallery.getSelectedItemPosition());
				MyMenu.getInstance(getApplicationContext()).addMenu(bean.name);
			}
		});
		myOrderBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(GalleryActivity.this,MenuListActivity.class));
			}
		});
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				CookBean bean = GlobalData.cookBook.get(pos).cooks
						.get(position);
				if (bean.bitmap != null) {
					is.setImageDrawable(new BitmapDrawable(GlobalData.cookBook
							.get(pos).cooks.get(position).bitmap));
				} else {
					try {
						if (Util.isExist(bean.uri))
							is.setImageDrawable(new BitmapDrawable(bean.uri));
						else
							is.setImageResource(R.drawable.pic);
					} catch (Exception e) {
						// TODO: handle exception
						is.setImageResource(R.drawable.pic);
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}

		});
		is.setOnTouchListener(new OnTouchListener() {
			/*
			 * 在ImageSwitcher控件上滑动可以切换图片
			 */
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getPointerCount() > 1) {
					Intent i = new Intent(GalleryActivity.this,
							TouchActivity.class);
					Bundle b = new Bundle();
					b.putInt("childId", gallery.getSelectedItemPosition());
					b.putInt("groupId", pos);
					i.putExtras(b);
					startActivity(i);
					return true;
				}
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					downX = (int) event.getX();// 取得按下时的坐标
					return true;
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					upX = (int) event.getX();// 取得松开时的坐标
					int index = 0;
					if (upX - downX > 100)// 从左拖到右，即看前一张
					{
						// 如果是第一，则去到尾部
						if (gallery.getSelectedItemPosition() == 0)
							index = gallery.getCount() - 1;
						else
							index = gallery.getSelectedItemPosition() - 1;
					} else if (downX - upX > 100)// 从右拖到左，即看后一张
					{
						// 如果是最后，则去到第一
						if (gallery.getSelectedItemPosition() == (gallery
								.getCount() - 1))
							index = 0;
						else
							index = gallery.getSelectedItemPosition() + 1;
					}
					// 改变gallery图片所选，自动触发ImageSwitcher的setOnItemSelectedListener
					gallery.setSelection(index, true);
					return true;
				}
				return false;
			}

		});
	}
}